package com.ursu.client.presenter;

import com.gwtplatform.dispatch.shared.DispatchAsync;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.ursu.client.place.NameTokens;
import com.ursu.shared.actions.Login;
import com.ursu.shared.actions.LoginResult;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.google.inject.Inject;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.gwtplatform.mvp.client.proxy.RevealRootContentEvent;

public class LoginPagePresenter extends
		Presenter<LoginPagePresenter.MyView, LoginPagePresenter.MyProxy> {
	
	private final EventBus eventBus;

	public interface MyView extends View {
		
		public TextBox getUsernameBox();
		public PasswordTextBox getPasswordBox();
		public Button getLoginBtn() ;
		public Label getErrorLabel();
	}

	@ProxyCodeSplit
	@NameToken(NameTokens.loginpage)
	public interface MyProxy extends ProxyPlace<LoginPagePresenter> {
	}

	@Inject
	PlaceManager placeManager;
	@Inject DispatchAsync dispatchAsync;
	@Inject
	public LoginPagePresenter(final EventBus eventBus, final MyView view,
			final MyProxy proxy) {
		super(eventBus, view, proxy);
		this.eventBus = eventBus;
	}

	@Override
	protected void revealInParent() {
		RevealRootContentEvent.fire(this, this);
	}

	@Override
	protected void onBind() {
		super.onBind();
		
		registerHandler(getView().getLoginBtn().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				
				checkCredentials();
			}
		}));
	}

	@Override
	protected void onReset() {
		super.onReset();
		
		
	}

	protected void checkCredentials() {
		String username = getView().getUsernameBox().getText();
		String password = getView().getPasswordBox().getText();
		
		Login action = new Login(username, password);
		dispatchAsync.execute(action, loginResultCallback);
			
	}
	private AsyncCallback<LoginResult> loginResultCallback = new AsyncCallback<LoginResult>() {

		@Override
		public void onSuccess(LoginResult result) {
			if(result.getCurrentUser() != null) {
				eventBus.fireEvent(new com.ursu.client.event.LoginAuthenticatedEvent(result.getCurrentUser()));
				
				PlaceRequest request = new PlaceRequest(NameTokens.getFirstpage());
				placeManager.revealPlace(request);
			}
			else {
			   //Authentication failed. Wrong username or password. Can be done with exceptions
			}
		}
		
		@Override
		public void onFailure(Throwable caught) {
			//Error while checking user credentials
			
		}

	};
}
